package com.oromwin.repos.server.request.volume;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;

import com.oromwin.repos.common.Logger;
import com.oromwin.repos.common.ReposException;
import com.oromwin.repos.common.helper.DBHelper;
import com.oromwin.repos.common.helper.IOHelper;
import com.oromwin.repos.data.AbstractReposTable;
import com.oromwin.repos.data.MediaTable;
import com.oromwin.repos.data.VolumeTable;
import com.oromwin.repos.server.LockObject;
import com.oromwin.repos.server.request.AbstractRequest;
import com.oromwin.repos.server.response.Response;
import com.oromwin.repos.server.response.volume.ListVolumeResponse;

public class ListVolumeRequest extends AbstractRequest {
    private Log log = Logger.getLog(ListVolumeRequest.class);
    private String VolumeId = null;

    @Override
    public void receive(DataInputStream dis, LockObject lockObject) throws ReposException {
        try {
            VolumeId = IOHelper.readUTF8(dis);
        } catch (IOException e) {
            throw ReposException.create(log, e);
        }
    }

    @Override
    public Response run() throws ReposException {
        ListVolumeResponse res = null;
        List<AbstractReposTable> reposTables = null;
        try {
            getCurrentReposTables();
            checkSameObjectExists();
            String whereClause = String.format("VolumeId = '%s' AND MediaStatus = 0", VolumeId);
            reposTables = DBHelper.selectTable(MediaTable.class, whereClause);
            res = new ListVolumeResponse(true, "Succeeded to list volume", reposTables);
        } catch (ReposException e) {
            if (reposTables == null) {
                reposTables = new ArrayList<AbstractReposTable>();
            }
            res = new ListVolumeResponse(false, e.getMessage(), reposTables);
        } catch (Exception e) {
            String message = String.format("Unexpected exception, {%s}", e.getMessage());
            throw ReposException.create(log, e, message);
        }
        return res;
    }

    @Override
    public void checkSameObjectExists() throws ReposException {
        String whereClause = String.format("VolumeId = '%s' AND VolumeStatus = 0", VolumeId);
        List<AbstractReposTable> list = DBHelper.selectTable(VolumeTable.class, whereClause);
        if (list.size() == 0) {
            String message = String.format("'%s' VolumeId does not exists in database", VolumeId);
            throw ReposException.create(log, message); 
        }
    }

    @Override
    public void getCurrentReposTables() throws ReposException {
        AbstractReposTable reposTable = null;
        String whereClause = String.format("VolumeStatus = 0 AND VolumeId = '%s'", VolumeId);
        List<AbstractReposTable> reposTables = DBHelper.selectTable(VolumeTable.class, whereClause);
        if (reposTables != null && reposTables.size() == 1) {
            reposTable = reposTables.get(0);
        } else {
            String message = String.format("'%s' VolumeId does not exists in database, tableSize:whereClause {%d:%s}", VolumeId, reposTables.size(), whereClause);
            throw ReposException.create(log, message); 
        }
        if (reposTable instanceof VolumeTable) {
            _volumeTable = (VolumeTable) reposTable;
        } else {
            String message = String.format("Illegal volume table type, class {%s}", reposTable.getClass().toString());
            throw ReposException.create(log, message);
        }
    }
}